import * as THREE from 'three';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js';
import {PhysicalController} from "../controller/PhysicalController";
import {HttpClient} from "../net/HttpClient";

import * as CANNON from 'cannon-es'

class Environment {
    constructor() {
        this.renderPool = [];
        this.clock = new THREE.Clock();
        this.httpClient = new HttpClient();
        this.scene = new THREE.Scene();
        this.physicalWorld = new CANNON.World({gravity: new CANNON.Vec3(0, -9.82, 0)});
        this.physicalWorld.broadphase = new CANNON.NaiveBroadphase();//NaiveBroadphase是默认的碰撞检测方式，该碰撞检测速度比较高

        this.scene.background = new THREE.Color(0xffffff);
        this.scene.fog = new THREE.Fog(0xffffff, 500, 1000);
        this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 100, 1000);
        this.camera.position.set(0, 75, 360);
        this.light = new THREE.DirectionalLight(0xffffff, 7);
        this.light.position.set(100, 150, 250);

        this.light.castShadow = true;

        this.light.shadow.mapSize.width = 1024;
        this.light.shadow.mapSize.height = 512;

        this.light.shadow.camera.near = 100;
        this.light.shadow.camera.far = 1200;

        this.light.shadow.camera.left = -1000;
        this.light.shadow.camera.right = 1000;
        this.light.shadow.camera.top = 350;
        this.light.shadow.camera.bottom = -350;
        this.scene.add(this.light);

        const axesHelper = new THREE.AxesHelper(200);
        this.scene.add(axesHelper);
        this.renderer = new THREE.WebGLRenderer();
        this.renderer.setSize(window.innerWidth, window.innerHeight);
        this.cameraControls = new OrbitControls(this.camera, this.renderer.domElement);

        /*this.cameraControls.enableRotate = false;
        this.cameraControls.enableZoom = false;*/
        document.body.appendChild(this.renderer.domElement);
        this.physicalController = new PhysicalController();
        this.initGlobalVariable();
    }

    initGlobalVariable() {
        window.scene = this.scene;
        window.camera = this.camera;
        window.renderer = this.renderer;
        window.light = this.light;
        window.clock = this.clock;
        window.deltaTime = clock.getDelta();
        window.physicalWorld = this.physicalWorld;
        window.cameraControls=this.cameraControls;
    }

    addRender(renderObject) {
        this.renderPool.push(renderObject);
    }

    render() {


        //this.physicalController.update();
        this.renderPool.forEach(item => item.update());
        let worldPosition = new THREE.Vector3();
        window.deltaTime = this.clock.getDelta();
        this.physicalWorld.step(deltaTime, 1000 / 60 );
        //this.cameraControls.getWorldPosition(worldPosition);
        this.camera.getWorldPosition(worldPosition);
        //console.log("center", worldPosition)
        this.renderer.render(this.scene, this.camera);
        const _this = this;
        requestAnimationFrame(function () {
            _this.render();
        });
    }
}

export {Environment}